﻿using CUREBusinessLogicLayer.Harmful;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

namespace CURE.Handler
{
    /// <summary>
    /// FileUploadHandler 的摘要说明
    /// </summary>
    public class FileUploadHandler : IHttpHandler
    {

        private ArrayList list = new ArrayList();
        private LedgerBll ledgerbll = new LedgerBll();

        public void ProcessRequest(HttpContext context)
        {
            string str = context.Request.QueryString["str"];
            int index=str.IndexOf("_");
            string id = str.Substring(0, index);
            string lid = str.Substring(index + 1, str.Length - index-1);
            //string lid = context.Request.QueryString["lid"];
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("expid", id);
            dic.Add("lid", lid);
            if (context.Request.Files.Count > 0)
            {
                HttpPostedFile ofile = context.Request.Files[0];
                if (Directory.Exists(context.Server.MapPath("~/files/"+id+"_"+lid)) == false)
                {
                    Directory.CreateDirectory(context.Server.MapPath("~/files/" + id + "_" + lid));
                }
                ofile.SaveAs(context.Server.MapPath("~/files/" + id + "_" + lid + "/" + ofile.FileName));
                dic.Add("path","/files/" + id + "_" + lid + "/" + ofile.FileName);
                UploadExp(dic);
                context.Response.Write("111");
            }
        }

        private bool UploadExp(Dictionary<string, object> dic)
        {
            return ledgerbll.UploadExp(dic);
        }

        #region 提示信息
        /// <summary>
        /// 操作失败(无参数)
        /// </summary>
        /// <returns></returns>
        public string failure()
        {
            return "{\"statusCode\":\"300\", \"message\":\"操作失败\"}";
        }
        /// <summary>
        /// 操作失败(有参数)
        /// </summary>
        /// <param name="str">错误提示</param>
        /// <returns></returns>
        public string failure(string str)
        {
            return "{\"statusCode\":\"300\", \"message\":\"操作失败!" + str + "\"}";
        }
        /// <summary>
        /// 操作成功(无参数)
        /// </summary>
        /// <returns></returns>
        public string success()
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!\",\"navTabId\":\"editLedger\", \"callbackType\":\"closeCurrent\"}";
        }

        public string success1()
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!\"}";
        }

        /// <summary>
        /// 操作成功(有参数)
        /// </summary>
        /// <param name="str">信息</param>
        /// <param name="navTabId">页面指向</param>
        /// <returns></returns>
        public string success(string str, string navTabId)
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!" + str + "\",\"navTabId\":\"" + navTabId + "\"}";
        }
        public string successfoward(string str, string url)
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!" + str + "\",\"navTabId\":\"forward\",\"forwardUrl\":\"" + url + "\"}";
        }
        public string success(string navTabId)
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!\",\"callbackType\":\"closeCurrent\",\"navTabId\":\"" + navTabId + "\"}";
        }
        #endregion

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}